package com.hackordie.interfaces;

import com.hackordie.commons.authorities.Profile;
import com.hackordie.commons.authorities.Role;
import com.hackordie.commons.authorities.User;
import com.hackordie.commons.authorities.UserAccess;
import com.hackordie.commons.game.player.UserComponent;
import com.hackordie.commons.hardware.components.AComponent;
import com.hackordie.commons.hardware.computer.ComputerType;
import com.hackordie.exceptions.HackOrDieException;
import com.hackordie.operations.AHackOrDieOperation;
import com.hackordie.operations.creates.CreateComponent;
import com.hackordie.operations.creates.CreateComputerType;
import com.hackordie.operations.creates.CreateProfile;
import com.hackordie.operations.creates.CreateRole;
import com.hackordie.operations.creates.CreateUser;
import com.hackordie.operations.deletes.DeleteComponent;
import com.hackordie.operations.deletes.DeleteComputerType;
import com.hackordie.operations.deletes.DeleteProfile;
import com.hackordie.operations.deletes.DeleteRole;
import com.hackordie.operations.deletes.DeleteUser;
import com.hackordie.operations.queries.QueryComponents;
import com.hackordie.operations.queries.QueryComputerType;
import com.hackordie.operations.queries.QueryCountComponents;
import com.hackordie.operations.queries.QueryCountComputerType;
import com.hackordie.operations.queries.QueryCountRoles;
import com.hackordie.operations.queries.QueryCountUsers;
import com.hackordie.operations.queries.QueryLoginUser;
import com.hackordie.operations.queries.QueryProfile;
import com.hackordie.operations.queries.QueryRole;
import com.hackordie.operations.queries.QueryUserComponents;
import com.hackordie.operations.queries.QueryUsers;
import com.hackordie.operations.updates.DisableRole;
import com.hackordie.operations.updates.DisableUser;
import com.hackordie.operations.updates.EnableRole;
import com.hackordie.operations.updates.EnableUser;
import com.hackordie.operations.updates.UpdateProfile;
import com.hackordie.operations.updates.UpdateRole;
import com.hackordie.operations.updates.UpdateUser;

public class HackOrDieManager implements IHackOrDieManager {

	private AHackOrDieOperation operation;

	public HackOrDieManager() {
		super();
	}

	@Override
	public boolean loginUser(String username, String password) throws HackOrDieException {
		operation = new QueryLoginUser(username, password);
		operation.execute();
		return true;
	}

	@Override
	public boolean createUser(User user) throws HackOrDieException {
		operation = new CreateUser(user);
		operation.execute();
		return true;
	}

	@Override
	public boolean deleteUser(int id) throws HackOrDieException {
		operation = new DeleteUser(id);
		operation.execute();
		return true;
	}

	@Override
	public boolean deleteUser(String username) throws HackOrDieException {
		operation = new DeleteUser(username);
		operation.execute();
		return true;
	}

	@Override
	public boolean updateUser(User user) throws HackOrDieException {
		operation = new UpdateUser(user);
		operation.execute();
		return true;
	}

	@Override
	public boolean enableUser(int id) throws HackOrDieException {
		operation = new EnableUser(id);
		return operation.execute().isCorrect(); //TODO hacer esto en todos			
	}

	@Override
	public boolean enableUser(String username) throws HackOrDieException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean disableUser(int id) throws HackOrDieException {
		operation = new DisableUser(id);		
		return operation.execute().isCorrect();
	}

	@Override
	public boolean disableUser(String username) throws HackOrDieException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public User getUser(int id) throws HackOrDieException {
		operation = new QueryUsers(id);
		return (User) operation.execute().getObject();
	}

	@Override
	public User getUser(String username) throws HackOrDieException {
		operation = new QueryUsers(username);
		return (User) operation.execute().getObject();
	}

	@Override
	public User[] getAllUsers() throws HackOrDieException {
		operation = new QueryUsers();
		return (User[]) operation.execute().getObject();
	}

	@Override
	public User[] getAllUsersByRange(int first, int last) throws HackOrDieException {
		operation = new QueryUsers(first, last);
		return (User[]) operation.execute().getObject();
	}

	@Override
	public int countAllUsers() throws HackOrDieException {
		operation = new QueryCountUsers();
		return (Integer) operation.execute().getObject();
	}

	@Override
	public UserAccess[] getUserAccess(int id) throws HackOrDieException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UserAccess[] getUserAccess(String username) throws HackOrDieException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UserAccess[] getAllAccess() throws HackOrDieException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean createProfile(Profile profile){
		operation = new CreateProfile(profile);
		try {
            return operation.execute().isCorrect();
        } catch (HackOrDieException e) {
            return false;
        }
	}

	@Override
	public boolean deleteProfile(int idProfile) throws HackOrDieException {
		operation = new DeleteProfile(idProfile);
		operation.execute();
		return true;
	}

	@Override
	public boolean updateProfile(Profile profile) throws HackOrDieException {
		operation = new UpdateProfile(profile);
		operation.execute();
		return true;
	}

	@Override
	public Profile getProfile(int idProfile) throws HackOrDieException {
		operation = new QueryProfile(idProfile);
		return (Profile) operation.execute().getObject();
	}

	@Override
	public Profile[] getAllProfiles() throws HackOrDieException {
		operation = new QueryProfile();
		return (Profile[]) operation.execute().getObject();
	}

    @Override
    public ComputerType[] getAllComputerType() throws HackOrDieException {
        operation = new QueryComputerType();
        return (ComputerType[]) operation.execute().getObject(); 
    }

    @Override
    public ComputerType[] getAllComputerTypeByRange(int first, int last) throws HackOrDieException {
        operation = new QueryComputerType(first, last);
        return (ComputerType[]) operation.execute().getObject();    
    }

    @Override
    public int countAllComputerType() throws HackOrDieException {
        operation = new QueryCountComputerType();
        return (Integer) operation.execute().getObject();
    }

    @Override
    public boolean deleteComputerType(int idComputerType)
            throws HackOrDieException {
        operation = new DeleteComputerType(idComputerType);        
        return operation.execute().isCorrect();
    }

    @Override
    public boolean addComputerType(ComputerType computerType) {
        operation = new CreateComputerType(computerType);
        try {
            return operation.execute().isCorrect();
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public AComponent[] getAllComponents() throws HackOrDieException {
        operation = new QueryComponents();
        return (AComponent[]) operation.execute().getObject(); 
    }

    @Override
    public AComponent[] getAllComponentsByRange(int first, int last)
            throws HackOrDieException {
        operation = new QueryComponents(first, last);
        return (AComponent[]) operation.execute().getObject(); 
    }

    @Override
    public int countAllComponents() throws HackOrDieException {
        operation = new QueryCountComponents();
        return (Integer) operation.execute().getObject();
    }

    @Override
    public boolean deleteComponents(int idComponent) throws HackOrDieException {
        operation = new DeleteComponent(idComponent);        
        return operation.execute().isCorrect();
    }

    @Override
    public boolean addComponents(AComponent component) {
        operation = new CreateComponent(component);
        try {
            return operation.execute().isCorrect();
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean createRole(Role role) {
        operation = new CreateRole(role);        
        
        try {
            return operation.execute().isCorrect();
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean deleteRole(int idRole) throws HackOrDieException {        
        operation = new DeleteRole(idRole);       
        return operation.execute().isCorrect();
    }

    @Override
    public boolean updateRole(Role role) throws HackOrDieException {
        operation = new UpdateRole(role);        
        return operation.execute().isCorrect();
    }

    @Override
    public Role getRole(int idRole) throws HackOrDieException {
        operation = new QueryRole(idRole);
        return (Role) operation.execute().getObject();
    }

    @Override
    public Role[] getRoleByRange(int first, int last) throws HackOrDieException {
        operation = new QueryRole(first, last);
        return (Role[]) operation.execute().getObject();
    }

    @Override
    public int countAllRoles() throws HackOrDieException {
        operation = new QueryCountRoles();
        return (Integer) operation.execute().getObject();
    }

    @Override
    public boolean enableRole(int idRole) throws HackOrDieException {
        operation = new EnableRole(idRole);
        return operation.execute().isCorrect();
    }

    @Override
    public boolean disableRole(int idRole) throws HackOrDieException {
        operation = new DisableRole(idRole);
        return operation.execute().isCorrect();
    }

    @Override
    public Role[] getAllRoles() throws HackOrDieException {
        operation = new QueryRole();
        return (Role[]) operation.execute().getObject();
    }

    @Override
    public Role getRole(String nameRole) throws HackOrDieException {
        operation = new QueryRole(nameRole);
        return (Role)operation.execute().getObject();        
    }

    @Override
    public UserComponent[] getUserBagComponents(int idUser, boolean allData)
            throws HackOrDieException {
        operation = new QueryUserComponents(idUser, allData);
        return (UserComponent[])operation.execute().getObject();
    }
}
